{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(Tensor(shape=[], dtype=Float32, value= 5),\n",
       " Tensor(shape=[], dtype=Float32, value= 6),\n",
       " Tensor(shape=[], dtype=Float32, value= 1.5),\n",
       " Tensor(shape=[], dtype=Float32, value= 9))"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import mindspore\n",
    "\n",
    "x = mindspore.Tensor(3.0)\n",
    "y = mindspore.Tensor(2.0)\n",
    "\n",
    "x + y, x * y, x / y, x**y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Tensor(shape=[4], dtype=Int64, value= [0, 1, 2, 3])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 这里需要用到mindspore的ops算子\n",
    "import mindspore.ops as ops\n",
    "\n",
    "x = ops.arange(4)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Tensor(shape=[], dtype=Int64, value= 3)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x[3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4,)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Tensor(shape=[5, 4], dtype=Int64, value=\n",
       "[[ 0,  1,  2,  3],\n",
       " [ 4,  5,  6,  7],\n",
       " [ 8,  9, 10, 11],\n",
       " [12, 13, 14, 15],\n",
       " [16, 17, 18, 19]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = ops.arange(20).reshape(5, 4)\n",
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Tensor(shape=[4, 5], dtype=Int64, value=\n",
       "[[ 0,  4,  8, 12, 16],\n",
       " [ 1,  5,  9, 13, 17],\n",
       " [ 2,  6, 10, 14, 18],\n",
       " [ 3,  7, 11, 15, 19]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Tensor(shape=[3, 3], dtype=Int64, value=\n",
       "[[1, 2, 3],\n",
       " [2, 0, 4],\n",
       " [3, 4, 5]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "B = mindspore.Tensor([[1, 2, 3], [2, 0, 4], [3, 4, 5]])\n",
    "B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Tensor(shape=[3, 3], dtype=Bool, value=\n",
       "[[ True,  True,  True],\n",
       " [ True,  True,  True],\n",
       " [ True,  True,  True]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "B == B.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Tensor(shape=[2, 3, 4], dtype=Int64, value=\n",
       "[[[ 0,  1,  2,  3],\n",
       "  [ 4,  5,  6,  7],\n",
       "  [ 8,  9, 10, 11]],\n",
       " [[12, 13, 14, 15],\n",
       "  [16, 17, 18, 19],\n",
       "  [20, 21, 22, 23]]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X = ops.arange(24).reshape(2, 3, 4)\n",
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(Tensor(shape=[5, 4], dtype=Float32, value=\n",
       " [[ 0.00000000e+00,  1.00000000e+00,  2.00000000e+00,  3.00000000e+00],\n",
       "  [ 4.00000000e+00,  5.00000000e+00,  6.00000000e+00,  7.00000000e+00],\n",
       "  [ 8.00000000e+00,  9.00000000e+00,  1.00000000e+01,  1.10000000e+01],\n",
       "  [ 1.20000000e+01,  1.30000000e+01,  1.40000000e+01,  1.50000000e+01],\n",
       "  [ 1.60000000e+01,  1.70000000e+01,  1.80000000e+01,  1.90000000e+01]]),\n",
       " Tensor(shape=[5, 4], dtype=Float32, value=\n",
       " [[ 0.00000000e+00,  2.00000000e+00,  4.00000000e+00,  6.00000000e+00],\n",
       "  [ 8.00000000e+00,  1.00000000e+01,  1.20000000e+01,  1.40000000e+01],\n",
       "  [ 1.60000000e+01,  1.80000000e+01,  2.00000000e+01,  2.20000000e+01],\n",
       "  [ 2.40000000e+01,  2.60000000e+01,  2.80000000e+01,  3.00000000e+01],\n",
       "  [ 3.20000000e+01,  3.40000000e+01,  3.60000000e+01,  3.80000000e+01]]))"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = ops.arange(20, dtype=mindspore.float32).reshape(5, 4)\n",
    "B = A.copy()\n",
    "A, A + B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Tensor(shape=[5, 4], dtype=Float32, value=\n",
       "[[ 0.00000000e+00,  1.00000000e+00,  4.00000000e+00,  9.00000000e+00],\n",
       " [ 1.60000000e+01,  2.50000000e+01,  3.60000000e+01,  4.90000000e+01],\n",
       " [ 6.40000000e+01,  8.10000000e+01,  1.00000000e+02,  1.21000000e+02],\n",
       " [ 1.44000000e+02,  1.69000000e+02,  1.96000000e+02,  2.25000000e+02],\n",
       " [ 2.56000000e+02,  2.89000000e+02,  3.24000000e+02,  3.61000000e+02]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A * B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(Tensor(shape=[2, 3, 4], dtype=Int64, value=\n",
       " [[[ 2,  3,  4,  5],\n",
       "   [ 6,  7,  8,  9],\n",
       "   [10, 11, 12, 13]],\n",
       "  [[14, 15, 16, 17],\n",
       "   [18, 19, 20, 21],\n",
       "   [22, 23, 24, 25]]]),\n",
       " (2, 3, 4))"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = 2\n",
    "X = ops.arange(24).reshape(2, 3, 4)\n",
    "a + X, (a * X).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(Tensor(shape=[4], dtype=Float32, value= [ 0.00000000e+00,  1.00000000e+00,  2.00000000e+00,  3.00000000e+00]),\n",
       " Tensor(shape=[], dtype=Float32, value= 6))"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = ops.arange(4, dtype=mindspore.float32)\n",
    "x, x.sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((5, 4), Tensor(shape=[], dtype=Float32, value= 190))"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A.shape, A.sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(Tensor(shape=[4], dtype=Float32, value= [ 4.00000000e+01,  4.50000000e+01,  5.00000000e+01,  5.50000000e+01]),\n",
       " (4,))"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A_sum_axis0 = A.sum(axis=0)\n",
    "A_sum_axis0, A_sum_axis0.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(Tensor(shape=[5], dtype=Float32, value= [ 6.00000000e+00,  2.20000000e+01,  3.80000000e+01,  5.40000000e+01,  7.00000000e+01]),\n",
       " (5,))"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A_sum_axis1 = A.sum(axis=1)\n",
    "A_sum_axis1, A_sum_axis1.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Tensor(shape=[], dtype=Float32, value= 190)"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A.sum(axis=(0, 1))  # 结果和A.sum()相同"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(Tensor(shape=[], dtype=Float32, value= 9.5),\n",
       " Tensor(shape=[], dtype=Float32, value= 9.5))"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A.mean(), A.sum() / A.numel()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(Tensor(shape=[4], dtype=Float32, value= [ 8.00000000e+00,  9.00000000e+00,  1.00000000e+01,  1.10000000e+01]),\n",
       " Tensor(shape=[4], dtype=Float32, value= [ 8.00000000e+00,  9.00000000e+00,  1.00000000e+01,  1.10000000e+01]))"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A.mean(axis=0), A.sum(axis=0) / A.shape[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Tensor(shape=[5, 1], dtype=Float32, value=\n",
       "[[ 6.00000000e+00],\n",
       " [ 2.20000000e+01],\n",
       " [ 3.80000000e+01],\n",
       " [ 5.40000000e+01],\n",
       " [ 7.00000000e+01]])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum_A = A.sum(axis=1, keepdims=True)\n",
    "sum_A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Tensor(shape=[5, 4], dtype=Float32, value=\n",
       "[[ 0.00000000e+00,  1.66666672e-01,  3.33333343e-01,  5.00000000e-01],\n",
       " [ 1.81818187e-01,  2.27272734e-01,  2.72727281e-01,  3.18181813e-01],\n",
       " [ 2.10526317e-01,  2.36842111e-01,  2.63157904e-01,  2.89473683e-01],\n",
       " [ 2.22222224e-01,  2.40740746e-01,  2.59259254e-01,  2.77777791e-01],\n",
       " [ 2.28571430e-01,  2.42857143e-01,  2.57142872e-01,  2.71428585e-01]])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A / sum_A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Tensor(shape=[5, 4], dtype=Float32, value=\n",
       "[[ 0.00000000e+00,  1.00000000e+00,  2.00000000e+00,  3.00000000e+00],\n",
       " [ 4.00000000e+00,  6.00000000e+00,  8.00000000e+00,  1.00000000e+01],\n",
       " [ 1.20000000e+01,  1.50000000e+01,  1.80000000e+01,  2.10000000e+01],\n",
       " [ 2.40000000e+01,  2.80000000e+01,  3.20000000e+01,  3.60000000e+01],\n",
       " [ 4.00000000e+01,  4.50000000e+01,  5.00000000e+01,  5.50000000e+01]])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A.cumsum(axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(Tensor(shape=[4], dtype=Float32, value= [ 0.00000000e+00,  1.00000000e+00,  2.00000000e+00,  3.00000000e+00]),\n",
       " Tensor(shape=[4], dtype=Float32, value= [ 1.00000000e+00,  1.00000000e+00,  1.00000000e+00,  1.00000000e+00]),\n",
       " Tensor(shape=[], dtype=Float32, value= 6))"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import mindspore.numpy as mnp\n",
    "\n",
    "y = ops.ones(4, dtype = mindspore.float32)\n",
    "x, y, mnp.dot(x, y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Tensor(shape=[], dtype=Float32, value= 6)"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ops.sum(x * y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((5, 4),\n",
       " (4,),\n",
       " Tensor(shape=[5], dtype=Float32, value= [ 1.40000000e+01,  3.80000000e+01,  6.20000000e+01,  8.60000000e+01,  1.10000000e+02]))"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A.shape, x.shape, mnp.dot(A, x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Tensor(shape=[5, 3], dtype=Float32, value=\n",
       "[[ 6.00000000e+00,  6.00000000e+00,  6.00000000e+00],\n",
       " [ 2.20000000e+01,  2.20000000e+01,  2.20000000e+01],\n",
       " [ 3.80000000e+01,  3.80000000e+01,  3.80000000e+01],\n",
       " [ 5.40000000e+01,  5.40000000e+01,  5.40000000e+01],\n",
       " [ 7.00000000e+01,  7.00000000e+01,  7.00000000e+01]])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "B = ops.ones((4, 3))\n",
    "ops.matmul(A, B)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Tensor(shape=[], dtype=Float32, value= 5)"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "u = mindspore.Tensor([3.0, -4.0], dtype=mindspore.float32)\n",
    "ops.norm(u, dim=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Tensor(shape=[], dtype=Float32, value= 7)"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ops.abs(u).sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Tensor(shape=[], dtype=Float32, value= 6)"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ops.norm(ops.ones((4, 9)), dim=(0, 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "MindSpore",
   "language": "python",
   "name": "mindspore"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
